<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Renting;
use App\Service\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LoginController extends Controller
{
    /**
     * @param Request $request
     * @return array
     * User: 张浩
     * Date: 2022/11/11
     * Time: 19:07
     * 获取用户数据
     */
    public function wxMyList(Request $request)
    {
        $uid = intval($request->get('uid',''));
        if (!$uid) return CommonService::fail(9999, '用户未登录', []);
        $user = Renting::rentingFirst($uid);
        if (empty($user)) return CommonService::fail(9999, '用户数据不存在', []);
        if (!empty($user['phone'])){
            $user['phone'] = CommonService::telHidden($user['phone']);
        }
        return CommonService::success(200, '用户数据获取成功', $user);
    }

    /**
     * @param Request $request
     * @return array|int
     * User: 张浩
     * Date: 2022/11/11
     * Time: 18:47
     * 用户修改密码
     */
    public function wxForgetPassword(Request $request)
    {
        $post = [
            'phone' => strval($request->get('phone','') ),
            'password' => strval($request->get('password','')),
            'code' => strval($request->get('code',''))
        ];
        //判断手机号是否为空格式是否正确
        $verify = CommonService::phoneCheck($post['phone']);
        if ($verify != 1) {
            return $verify;
        } else {
            //判断验证码是否正确
            $codes = Cache::get('shq' . $post['phone']);
            if ($post['code'] != $codes) return CommonService::fail(9999, '验证码不正确', []);
            $user = Renting::phoneRentingfirst($post['phone']);
            if (empty($user)) {
                return CommonService::fail(9999, '没有这条数据', []);
            } else {
                //修改用户密码
                $list= Renting::wxForgetPasswor($post, $user);
                if ($list) return CommonService::success(200,'密码修改成功',$list);
                return CommonService::fail(9999,'密码修改失败',[]);
            }
        }
    }

    /**
     * @param $post
     * @return array
     * User: 张浩
     * Date: 2022/11/11
     * Time: 14:28
     * 手机号密码登录
     */
    public function wxPhoneLogin(Request $request)
    {
        $post = [
            'phone' => strval($request->get('phone','') ),
            'password' => strval($request->get('password',''))
        ];
        //判断用户请求次数是否过多
        $num = Cache::get($post['phone'] . 'user');
        if ($num > 3) return CommonService::fail(9999, '请5分钟后再试', []);
        $user = Renting::phoneRentingfirst($post['phone']);
        //判断用户输入的账号是否正确
        if (empty($user)) {
            Cache::put($post['phone'] . 'user', $num + 1, 60 * 5);
            return CommonService::fail(9999, '用户不存在', '还有' . (3 - $num) . '次机会');
        }
        if (md5($post['password']) != $user['password']) {
            Cache::put($post['phone'] . 'user', $num + 1, 60 * 5);
            return CommonService::fail(9999, '密码错误', '还有' . (3 - $num) . '次机会');
        }
        //判断用户是否是团长
        if ($user['is_group']){
            $token = CommonService::createToken($user['id']);
            if ($token) {
                Cache::put('qiweitoken', $token);
                $data=[
                    'token'=>$token,
                    'uid'=>$user['id']
                ];
                return CommonService::success(200, '团长登陆成功', $data);
            }
            return CommonService::fail(9999, '登录失败', []);
        }
        $token = CommonService::createToken($user['id']);
        if ($token) {
            Cache::put('qiweitoken', $token);
            $data=[
                'token'=>$token,
                'uid'=>$user['id']
            ];
            return CommonService::success(200, '登陆成功', $data);
        }
        return CommonService::fail(9999, '登录失败', []);
    }

    /**
     * @param Request $request
     * @return array
     * @throws \Throwable
     * User: 张浩
     * Date: 2022/11/11
     * Time: 13:44
     * 注册新用户
     */
    public function wxEnrollLogin(Request $request)
    {
        $post = [
            'phone' => strval($request->get('phone','') ),
            'code' => strval($request->get('code','')),
            'password' => strval($request->get('password',''))
        ];
        //判断验证码是否正确
        $code = Cache::get('shq' . $post['phone']);
        if ($code != $post['code']) return CommonService::fail(9999, '验证码不正确', []);
        //判断用户是否填写邀请码
        if (!empty($post['place_code'])) {
            $user = self::wxPlaceCode($post);
            return $user;
        }
        //根据手机号获取用户信息
        $user = Renting::where('phone', $post['phone'])->first();
        if (empty($user)) {
            $user = Renting::wxEnrollLogin($post);
            return $user;
        }
        return CommonService::fail(9999, '已有账号请登录', []);
    }

    /**
     * @param $post
     * @return array
     * @throws \Throwable
     * User: 张浩
     * Date: 2022/11/11
     * Time: 13:42
     * 用户注册时填写注册吗时的操作
     */
    public static function wxPlaceCode(Request $request)
    {
        $post = $request->all();
        //根据手机号获取一条数据
        $user = Renting::phoneRentingfirst($post['phone']);
        //判断是否获取到数据
        if (empty($user)) {
            //未获取到数据时进行添加
            $user = Renting::wxPlaceCode($post);
            return $user;
        }
    }


    /**
     * @param Request $request
     * @return array|int
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * User: 张浩
     * Date: 2022/11/11
     * Time: 9:27
     * 手机号短信验证码发送
     */
    public function wxEnroll(Request $request)
    {
        $phone = strval($request->get('phone',''));
        //验证手机号是否为空手机格式是否正确
        $verify = CommonService::phoneCheck($phone);
        if ($verify != 1) {
            return $verify;
        } else {
            //接口限制
            $port = CommonService::requestThrottle($phone);
            if ($port != null) {
                return $port;
            } else {
                $user=rand(9999,1111);
                Cache::put('shq'.$phone,$user,5*60);
                return commonService::success(200, '成功', $user);
                //第三方短信验证码发送
//                $user = CommonService::sendSms($phone);
//                return $user;
            }
        }
    }

    /**
     * @param Request $request
     * @return array
     * User: 张浩
     * Date: 2022/11/10
     * Time: 19:37
     * 微信授权登录
     */
    public function wxLogin(Request $request)
    {
        //获取前端传来的值
        $log = strval($request->get('log', ''));
        $nickName = strval($request->get('nickName', ''));
        $code = strval($request->get('code', ''));
        //根据curl请求数据
        $url = config('wx.url_html') . $code . config('wx.url_code');
        $urls = CommonService::geturl($url);
        if (empty($urls['session_key']) || empty($urls['openid'])) return commonService::fail(9999,'登录失败',[]);
        //组装数据
        $post = [
            'session_key' => $urls['session_key'],
            'openid' => $urls['openid'],
            'avatar' => $log,
            'code' => $code,
            'nickName' => $nickName
        ];
        $result = Renting::wxLogin($post);
        return $result;
    }
}
